<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"%> 
<%@ Page Language="C#"  MasterPageFile="~/_layouts/application.master"   EnableViewState="false" EnableViewStateMac="false"   %>


<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %> 

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 


<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
  }

protected void cmdApplyCustomBrand_Click(object sender, EventArgs e) {
  
  SPWeb site = SPContext.Current.Web;
  
  string MasterUrlPath = site.ServerRelativeUrl;
  if (!MasterUrlPath.EndsWith(@"/"))
    MasterUrlPath += @"/";
  MasterUrlPath += @"_catalogs/masterpage/PropertyManagement.master";
  ApplyCustomBrand(MasterUrlPath, site);
  
  Response.Redirect(Request.RawUrl);
}

protected void ApplyCustomBrand(string MasterUrlPath, SPWeb site) {
  site.ApplyTheme("");
  site.MasterUrl = MasterUrlPath;
  site.AlternateCssUrl = "/_layouts/1033/STYLES/PropertyManagement/EstateAgent.css";
  site.Update();
  
  foreach (SPWeb child in site.Webs) {
    ApplyCustomBrand(MasterUrlPath, child);
  }
}

  protected void cmdRemoveCustomBrand_Click(object sender, EventArgs e) {
    SPWeb site = SPContext.Current.Web;
    RemoveCustomBrand(site);    
    Response.Redirect(Request.RawUrl);   
  }

  protected void RemoveCustomBrand(SPWeb site) {
    string MasterUrlPath = site.ServerRelativeUrl;
    if (!MasterUrlPath.EndsWith(@"/"))
      MasterUrlPath += @"/";
    site.MasterUrl = MasterUrlPath + "_catalogs/masterpage/default.master";    
    site.AlternateCssUrl = "";
    site.SiteLogoUrl = "";
    site.Update();
    foreach (SPWeb child in site.Webs) {
      RemoveCustomBrand(child);
    }    
  }
</script>

<asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server">

  <hr />

  <asp:Button ID="cmdApplyCustomBrand" runat="server" Text="Apply Custom Branding" OnClick="cmdApplyCustomBrand_Click" />
  <br />
  <br />
  <asp:Button ID="cmdRemoveCustomBrand" runat="server" Text="Remove Custom Branding" OnClick="cmdRemoveCustomBrand_Click"  /><br />

  <hr/>
  

</asp:Content>

